package com.neuedu.demoweb.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.demoweb.bean.Admin;
import com.neuedu.demoweb.common.Pager;
import com.neuedu.demoweb.common.WebException;
import com.neuedu.demoweb.dao.AdminDao;
import com.neuedu.demoweb.utils.DbUtil;



/**
 * 
 * @author chens
 *
 */
public class AdminDaoImpl implements AdminDao {

	/**
	 * 为了数据库事务控制需要，Dao层不打开数据库连接，而是接受从业务层传递的connection
	 */
	private Connection conn;

	@Override
	public boolean add(Admin object) throws WebException {

		boolean ret = false;
		PreparedStatement stmt = null;
		try {
			// 3.4.
			String sql = "insert into admin(adminName,password,salt,adminImage) values(?,?,?,?)";
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, object.getAdminName());
			stmt.setString(2, object.getPassword());
			stmt.setString(3, object.getSalt());
			stmt.setString(4, object.getAdminImage());
			int count = stmt.executeUpdate();
			if (count == 1) {
				ret = true;
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("新增管理员异常");
		} finally {
			DbUtil.close(null, stmt, null);
		}

		return ret;
	}

	@Override
	public boolean edit(Admin object) throws WebException {

		boolean ret = false;
		PreparedStatement stmt = null;
		try {
			// 3.4.
			String sql = "update  admin set adminName=?,password=?,salt=?,adminImage=? where adminId=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, object.getAdminName());
			stmt.setString(2, object.getPassword());
			stmt.setString(3, object.getSalt());
			stmt.setString(4, object.getAdminImage());
			stmt.setInt(5, object.getAdminId());
			int count = stmt.executeUpdate();

			if (count == 1) {
				ret = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("修改管理员异常");
		} finally {
			DbUtil.close(null, stmt, null);
		}

		return ret;
	}

	@Override
	public boolean deleteById(Integer id) throws WebException {

		boolean ret = false;
		// 超级管理员不能删除
		if (id != null && id == 1) {
			throw new WebException("默认管理员不能删除");
		}

		PreparedStatement stmt = null;
		try {
			// 3.4.
			String sql = "delete  from   admin  where adminId=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			int count = stmt.executeUpdate();

			if (count == 1) {
				ret = true;
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("删除管理员异常");
		} finally {
			DbUtil.close(null, stmt, null);
		}

		return ret;
	}

	@Override
	public List<Admin> findList() throws WebException {

		List<Admin> list = new ArrayList<Admin>();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 3.4.
			String sql = "select adminId,adminName,password,adminImage  from   admin  ";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();

			while (rs.next()) {
				Integer adminId = rs.getInt(1);
				String adminName = rs.getString(2);
				String password = rs.getString(3);
				String adminImage = rs.getString(4);
				list.add(new Admin(adminId, adminName, password,null,adminImage));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("查询管理员列表异常");
		} finally {
			DbUtil.close(null, stmt, rs);
		}

		return list;
	}

	@Override
	public Admin findById(Integer id) throws WebException {

		Admin admin = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 3.4.
			String sql = "select adminId,adminName,password,salt,adminImage  from   admin where adminId = ? ";
			stmt = conn.prepareStatement(sql);

			stmt.setInt(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
				Integer adminId = rs.getInt(1);
				String adminName = rs.getString(2);
				String password = rs.getString(3);
				String salt = rs.getString(4);
				String adminImage = rs.getString(5);
				admin = new Admin(adminId, adminName, password,salt,adminImage);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("查询管理员异常");
		} finally {
			DbUtil.close(null, stmt, rs);
		}

		return admin;
	}

	@Override
	public List<Admin> search(Pager<Admin> pager) throws WebException {

		List<Admin> list = new ArrayList<Admin>();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 3.4.只进行分页查询,没有实现条件查询
			String sql = "select adminId,adminName,password,salt,adminImage  from   admin  order by  adminId desc limit ?,? ";
			String sql2 = "select adminId,adminName,password,salt,adminImage  from   admin where  adminName like ?   order by  adminId desc  limit ?,? ";

			if (pager.getCondition() == null || pager.getCondition().getAdminName() == null) {
				stmt = conn.prepareStatement(sql);
			} else {
				stmt = conn.prepareStatement(sql2);
			}
			if (pager.getCondition() == null || pager.getCondition().getAdminName() == null) {
				stmt.setInt(1, pager.getStart());
				stmt.setInt(2, pager.getSize());
			} else {
				stmt.setString(1, "%" + pager.getCondition().getAdminName() + "%");
				stmt.setInt(2, pager.getStart());
				stmt.setInt(3, pager.getSize());
			}
			rs = stmt.executeQuery();
			while (rs.next()) {
				Integer adminId = rs.getInt(1);
				String adminName = rs.getString(2);
				String password = rs.getString(3);
				String salt = rs.getString(4);
				String adminImage = rs.getString(5);
				list.add(new Admin(adminId, adminName, password,null,adminImage));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("分页查询管理员列表异常");
		} finally {
			DbUtil.close(null, stmt, rs);
		}

		return list;
	}

	@Override
	public int findTotal(Pager<Admin> pager) throws WebException {
		int total = 0;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 3.4.没有实现条件查询
			String sql = "select count(*)  total from   admin ";
			String sql2 = "select count(*) from   admin where  adminName like ? ";
			if (pager.getCondition() == null || pager.getCondition().getAdminName() == null) {
				stmt = conn.prepareStatement(sql);
			} else {
				stmt = conn.prepareStatement(sql2);
			}
			if (pager.getCondition() != null && pager.getCondition().getAdminName() != null) {
				stmt.setString(1, "%" + pager.getCondition().getAdminName() + "%");
			}
			rs = stmt.executeQuery();
			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("查询管理员记录总数异常");

		} finally {
			DbUtil.close(null, stmt, rs);
		}
		return total;
	}

	/**
	 * 从业务层接收连接
	 */
	@Override
	public void setConnection(Connection conn) {
		this.conn = conn;
	}

	@Override
	public Admin login(Admin admin) throws WebException {
		Admin tempAdmin = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 使用盐值算法后，系统的密码不能直接进行判断，只能先通过账号查询用密码和盐，在程序中进行判断
			String sql = "select adminId,adminName,password,salt,adminImage  from   admin where adminName=?  ";
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, admin.getAdminName());

			rs = stmt.executeQuery();
			if (rs.next()) {
				Integer adminId = rs.getInt(1);
				String adminName = rs.getString(2);
				String password = rs.getString(3);
				String salt = rs.getString(4);
				String adminImage = rs.getString(5);
				tempAdmin = new Admin(adminId, adminName, password,salt,adminImage);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebException("管理员登录异常");
		} finally {
			DbUtil.close(null, stmt, rs);
		}

		return tempAdmin;
	}
}
